<template>
    <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px"
             class="demo-ruleForm login-container">
        <h3 class="title">{{ $t('operation.Systemlogin') }}</h3>
        <el-form-item prop="account">
            <el-input type="text" v-model="ruleForm2.account" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
            <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-checkbox v-model="checked" checked class="remember">{{ $t('operation.Remember') }}</el-checkbox>
        <el-form-item style="width:100%;">
            <el-button type="primary" style="width:48%;" @click="handleSubmit2">{{
                $t('operation.Login') }}
            </el-button>
            <el-button type="primary" style="width:48%;" @click="settledIn">{{
                $t('operation.Settled') }}
            </el-button>
            <!--<el-button @click="handleReset2">重置</el-button>-->
        </el-form-item>
        <el-form-item style="width:100%;">
            <el-button type="success" style="width:100%;" @click="changeLang">{{
                $t('operation.Chinese_English_switch') }}
            </el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    // import {requestLogin} from '../api/api';
    //import NProgress from 'nprogress'
    export default {
        data() {
            return {
                logining: false,
                ruleForm2: {
                    account: '测试2',
                    checkPass: '1',
                    username: '',
                    password: '',
                },
                rules2: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                        //{ validator: validaePass }
                    ],
                    checkPass: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        //{ validator: validaePass2 }
                    ]
                },
                checked: true,
                shop: ''
            };
        },
        methods: {
            //国际化
            changeLang() {
                console.log(this.$i18n.locale)
                // 判断当前的local为什么,取反
                let lang = this.$i18n.locale === 'zh-CN' ? 'en-US' : 'zh-CN'
                this.$i18n.locale = lang
            },
            //入驻
            settledIn() {
                // 跳转到指定的组件
                this.$router.push({path: '/shopReg'});
            },
            //登陆
            handleSubmit2(ev) {
                var _this = this;
                this.$refs.ruleForm2.validate((valid) => {
                    if (valid) {
                        var loginParams = {
                            username: this.ruleForm2.account,
                            password: this.ruleForm2.checkPass,
                            type: 0
                        };
                        this.$http.post("/login", loginParams).then(res => {

                            //NProgress.done();
                            let {message, success, resultObj} = res.data;
                            console.log(message, success, resultObj)
                            if (!success) {
                                this.$message({
                                    message: message,
                                    type: 'error'
                                });
                            } else {
                                //把token存放到浏览器中
                                localStorage.setItem('token', resultObj.token);
                                //把用户放到浏览器中,转为json字符串，不然存储为一个对象,如果返回只有一个token不用转
                                localStorage.setItem("user", JSON.stringify(resultObj.user))

                                // this.$router.push({path: '/'});

                                if (resultObj.loginUser.shop && resultObj.loginUser.shop.state !== 2) {
                                    //存储前端的session
                                    sessionStorage.setItem('user', JSON.stringify(resultObj.loginUser));
                                    this.$router.push({path: '/homepage'});
                                } else {
                                    this.$router.push({path: '/'});
                                }

                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },


            //扩展登陆
            login1() {
                this.$http.post('/user/login', this.ruleForm2).then(res => {
                    console.log(res)
                    let {message, resultObj, success} = res.data; //解构函数

                    if (resultObj.shop && resultObj.shop.state !== 2) {
                        //存储前端的session
                        sessionStorage.setItem('user', JSON.stringify(resultObj));
                        this.$router.push({path: '/homepage'});
                    } else {
                        this.$router.push({path: '/'});
                    }
                })
            }
        }
    }

</script>

<style lang="scss" scoped>
    .login-container {
        /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;

        .title {
            margin: 0px auto 40px auto;
            text-align: center;
            color: #505458;
        }

        .remember {
            margin: 0px 0px 35px 0px;
        }
    }
</style>
